//
//  WVMeasurer.m
//  Windows and Views
//
//  Created by Tim Isted on 07/10/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "WVMeasurer.h"

NSString * const WVMeasurerShowMeasurementsNotification = @"WVMeasurerShowMeasurementsNotification";

@implementation WVMeasurer

- (void)awakeFromNib
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self 
               selector:@selector(windowDidResize:) 
                   name:NSWindowDidResizeNotification 
                 object:applicationWindow];
    
    [center addObserver:self
               selector:@selector(needToShowMeasurements:) 
                   name:WVMeasurerShowMeasurementsNotification
                 object:nil];
    
/*    [center addObserver:self 
               selector:@selector(receivedNotification:) 
                   name:nil 
                 object:nil];*/
}

/*- (void)receivedNotification:(NSNotification *)notification
{
    NSLog(@"Notification: %@", notification);
}*/

- (void)needToShowMeasurements:(NSNotification *)notification
{
    NSRect windowFrame = [applicationWindow frame];
    
    NSPoint windowLocation = windowFrame.origin; 
    NSSize windowSize = windowFrame.size;
    
    [windowLocationTextField setStringValue:NSStringFromPoint(windowLocation)];
    [windowSizeTextField setStringValue:NSStringFromSize(windowSize)];
    
    NSRect viewFrame = [mainView frame];
    
    NSPoint viewLocation = viewFrame.origin;
    NSSize viewSize = viewFrame.size;
    
    [viewLocationTextField setStringValue:NSStringFromPoint(viewLocation)];
    [viewSizeTextField setStringValue:NSStringFromSize(viewSize)];
}

- (IBAction)showMeasurements:(id)sender
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center postNotificationName:@"WVMeasurerShowMeasurementsNotification"
                          object:self];
}

- (void)windowDidResize:(NSNotification *)notification
{
    NSLog(@"Notification: %@", notification);
    [self showMeasurements:[notification object]];
}

- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
    frameSize.width = (frameSize.height * 2);
    
    return frameSize;
}

@end
